from django.db import models
from django.utils.html import format_html

# Create your models here.



class BooksInfo(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField('书籍名称', max_length=100)
    types = models.CharField('书籍类型', max_length=100)
    readCount = models.IntegerField('阅读次数')
    likes = models.IntegerField('收藏数量')
    rating = models.FloatField('评分')
    img = models.FileField('书籍主图', upload_to=r'imgs')
    details = models.FileField('书籍内容', upload_to=r'details', max_length=10000)

    def __str__(self):
        return str(self.id)

    class Meta:
        verbose_name = '书籍信息'
        verbose_name_plural = verbose_name

    # 自定义函数，设置字体颜色
    def colored_name(self):
        if '传记' in self.types:
            color_code = 'red'
        else:
            color_code = 'blue'
        return format_html(
            '<span style="color: {};">{}</span>',
            color_code,
            self.types,
        )
    colored_name.short_description = '带颜色的商品类型'

# class RatingInfo(models.Model):
#     id = models.AutoField(primary_key=True)
#     books_id = models.IntegerField('书籍ID')
#     rating_cont



